package com.ynhenc.pta.routeRank.impl;

import com.ynhenc.comm.*;
import com.ynhenc.comm.db.*;

public class RecordRoute {

	public RecordRoute(QueryResult qr) {

		this.xType = qr.getDouble("xType");
		this.sRouteID = qr.getString("sRouteId");
		this.sRouteNo = qr.getString("sRouteNo");
		this.sStationID = qr.getString("sStationID");
		this.sStationName = qr.getString("sStationName");
		this.routeSeq_A1 = qr.getInteger("routeSeq_A1");
		this.routeSeq_A2 = qr.getInteger("routeSeq_A2");
		this.tStationID = qr.getString("tStationID");
		this.tStationName = qr.getString("tStationName");
		this.eRouteID = qr.getString("eRouteID");
		this.eRouteNo = qr.getString("eRouteNo");
		this.routeSeq_B1 = qr.getInteger("routeSeq_B1");
		this.routeSeq_B2 = qr.getInteger("routeSeq_B2");
		this.eStationID = qr.getString("eStationID");
		this.eStationName = qr.getString("eStationName");
		this.length = qr.getDouble("length");
		this.sDistance = qr.getDouble("sDistance");
		this.eDistance = qr.getDouble("eDistance");
		this.distance = qr.getDouble("distance");
		this.times = qr.getDouble("times"); 

	}

	public double getTotalDistance_Meter() {
		return this.length; // do not use sDistance(from orgin to first
							// statsion) or eDistiance( from last station to
							// destination)
	}
	
	private double getPartialDistance_Meter( int pos ) {
		double dist_Meter = this.getTotalDistance_Meter() ;
		if ( dist_Meter <= 0.0 ) {
			return 0;
		} else {
			double diffSeqA = Math.abs( this.routeSeq_A2 - this.routeSeq_A1 );
			double diffSeqB = Math.abs( this.routeSeq_B2 - this.routeSeq_B1 );
			if( diffSeqA + diffSeqB <= 0 ) {
				return 0;
			} else {
				return dist_Meter*( pos == 0 ? diffSeqA : diffSeqB ) /(diffSeqA + diffSeqB);
			} 
		} 
	} 

	public double getSTDistance_Meter() {
		return this.getPartialDistance_Meter( 0 );
	}
	
	public double getTEDistance_Meter() {
		return this.getPartialDistance_Meter( -1 );
	}
	
	public double getTotalDuration_Sec() {
		return this.times*60;
	}
	
	private double getPartialDuration_Sec( int pos ) {
		double dist_Meter = this.getTotalDistance_Meter() ;
		if( dist_Meter <= 0 ) {
			return 0;
		} else {
			double distPart_Meter = this.getPartialDistance_Meter(pos);
			return this.getTotalDuration_Sec()*distPart_Meter/dist_Meter;
		}
	}
	
	public double getSTDuration_Sec() {
		return this.getPartialDuration_Sec( 0 );
	}
	
	public double getTEDuration_Sec() {
		return this.getPartialDuration_Sec( -1 );
	}

	double xType;
	String sRouteID;
	String sRouteNo;
	String sStationID;
	String sStationName;
	int routeSeq_A1;
	int routeSeq_A2;
	String tStationID;
	String tStationName;
	String eRouteID;
	String eRouteNo;
	int routeSeq_B1;
	int routeSeq_B2;
	String eStationID;
	String eStationName;
	double length;
	double sDistance;
	double eDistance;
	private double distance;
	//double sTimes;
	//double eTimes;
	private double times;

	public String toString() {
		return "xType(" + xType + ") " + "sRouteId(" + sRouteID + ") " + "sRouteNo(" + sRouteNo + ") " + "sStationName(" + sStationName + ") " + "routeSeq_A1(" + routeSeq_A1 + ") " + "routeSeq_A2("
				+ routeSeq_A2 + ") " + "tStationName(" + tStationName + ") " + "eRouteID(" + eRouteID + ") " + "routeSeq_B1(" + routeSeq_B1 + ") " + "routeSeq_B2(" + routeSeq_B2 + ") "
				+ "eStationName(" + eStationName + ") " + "length(" + length + ") " + "sDistance(" + sDistance + ") " + "eDistance(" + eDistance + ") " + "distance(" + distance + ") " + "times("
				+ times + ") ";
	}

}
